/*
 * @Author: 啊彪
 * @Date: 2023-12-29 13:59:35
 * @LastEditors: 啊彪
 * @LastEditTime: 2023-12-29 16:31:51
 * @Description: 
 * Copyright (c) 2023 by 2282059276@qq.com, All Rights Reserved. 
 */
/**
 * @Author: 陈瑞鹏
 * @Date:   2022-10-14 15:36:27
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-12-05 09:31:40
 */
import cors from 'cors';
import express from 'express';
import 'express-async-errors';
import path from 'path';
import { route } from './config/api.config';
import configDefault from './config/config.default';
import middleware from './controllers/middleware.controller';
import server from './controllers/server.controller';
import statistics from './controllers/statistics.controller';
import guardRoute from './guard/guard.route';
import gameRoute from './miniGames/game.route';
import equipRoute from './routes/equip.route';
import heroRoute from './routes/hero.route';
import loginRoute from './routes/login.route';
import signRoute from './routes/sign.route';
import taskRoute from './routes/task.route';
import userRoute from './routes/user.route';
import bigNumber from './utils/bigNumber';
import './utils/mongo';
import socket from './utils/socket/socket';

const app = express();

/**
 * socket
 */
const sot = new socket(Number(bigNumber.jia(2000, process.env.pm_id || 0)));
sot.onterRoom()

app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, '../public/')));
app.set("views", path.join(__dirname, '../views'));
app.set('view engine', 'html');
app.use(cors(configDefault.cors));

app.use(middleware.intercept); //拦截
app.use(middleware.requestLogs); //请求日志
app.use(middleware.check_api_token); //校验token

app.use(route.login, loginRoute);
app.use(route.user, userRoute);
app.use(route.hero, heroRoute);
app.use(route.sign, signRoute);
app.use(route.task, taskRoute);
app.use(route.equip, equipRoute);

app.use('/game', gameRoute);

app.use('/guard', guardRoute);


/**
 * 异常捕获
 */
app.use((err: any, req: any, res: any, next: any) => {

  // console.log("异常捕获")
  if (err) {
    if (!err.code && err.code != 0) {
      console.log(err)
    }
    return res.json({ code: err.code, message: err.message, data: err.data });
  }
});

let port = bigNumber.jia(configDefault.appPort, process.env.pm_id || 0) || configDefault.appPort;

app.listen(port, async () => {

  switch (process.env.pm_id) {
    case '0':

      console.log("process.env.pm_id");

      await server.serverInit();

      await statistics.serverStatistics();

      await middleware.init_switch();
      break;

  }
  console.log(`[${process.env.GAME_NAME}] [${process.env.ENV}] 启动成功 http://127.0.0.1:${port}`)

  //发送邮件 sendType 1 为 个人 2为全体
  // guard.sendEmail(164, 2, 2, "游戏公告", "游戏目前属于玩法demo测试阶段，因为我们不是很清楚这类玩法是否能让大众接受，如果您对游戏后续开发有不错的建议可以加Q群：439976203", '[]');
  // guard.sendEmail(146, 1, 1, "测试", "金币x50", '[{\"type\":\"1\",\"min\":\"200\",\"max\":\"200\"},{\"type\":\"3\",\"num\":\"2\"}]');
  // guard.sendExchange("8888", 1, '[{\"type\":\"1\",\"min\":\"2000\",\"max\":\"2000\"}]');
  // guard.sendExchange("9999", 1, '[{\"type\":\"1\",\"min\":\"2000\",\"max\":\"2000\"},{\"type\":\"3\",\"num\":\"2\"}]');
  // let json: any = await utils.get_all_json("guard/exchangeArr.json");
  // console.log(json);
  // json.forEach(async (element, i) => {
  //   if (element.type == 2) {
  //     for (let j = 0; j < element.num; j++) {
  //       let rand = utils.get_rand_string(6);
  //       let r = await Exchange.findOne({ id: rand });
  //       // console.log(r)
  //       if (!r) {
  //         await Exchange.updateOne({ id: 8000 + j + "" }, { id: rand })
  //       }
  //     }
  //   }
  // });
  // let userArr = await GUser.find({});
  // // console.log(userArr);
  // userArr.forEach(async (ele: any) => {
  //   await GUser.updateOne({ roleId: ele.roleId }, { worldId: Math.ceil(ele.roleId / 50) })
  // })
})


